| Conditions | 9 |
| Paths | 33 |
| Total Lines | 73 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /* TODO |
||
| 116 | app.get('/exams/', function (req, res, next) {
|
||
| 117 | if (!req.query.id || !req.query.pwd || (req.query.sem && !(/^20\d{2}-20\d{2}-[1-2]$/).test(req.query.sem))) {
|
||
| 118 | res.send({ error: "参数不正确" });
|
||
| 119 | return; |
||
| 120 | } |
||
| 121 | if (program.fullLog) {
|
||
| 122 | var start = new Date(); |
||
| 123 | console.log((timeStamp() + 'Started to query the exams: ').cyan + req.query.id.yellow); |
||
|
1 ignored issue
–
show
|
|||
| 124 | } |
||
| 125 | // 获取想要查询的学期,如果没有指定以系统时间为准 |
||
| 126 | var sem; |
||
| 127 | if (req.query.sem) {
|
||
| 128 | sem = req.query.sem; |
||
| 129 | } else {
|
||
| 130 | let now = new Date(); |
||
| 131 | let month = now.getMonth(); |
||
| 132 | let year = now.getFullYear(); |
||
| 133 | if (month === 0) {
|
||
| 134 | sem = (year - 1) + '-' + year + '-1'; |
||
| 135 | } else if (month <= 6) {
|
||
| 136 | sem = (year - 1) + '-' + year + '-2'; |
||
| 137 | } else {
|
||
| 138 | sem = year + '-' + (year + 1) + '-1'; |
||
| 139 | } |
||
| 140 | } |
||
| 141 | access.login(req.query.id, req.query.pwd, res, function (headers, ires) {
|
||
| 142 | var ret = {};
|
||
| 143 | var $ = cheerio.load(ires.text); |
||
| 144 | ret.name = escaper.unescape($('.block1text').html()).match(/姓名:.+</)[0].replace('<', '').substring(3);
|
||
| 145 | ret.id = req.query.id; |
||
| 146 | ret.sem = sem; |
||
| 147 | |||
| 148 | superagent.post('http://csujwc.its.csu.edu.cn/jsxsd/xsks/xsksap_list')
|
||
| 149 | .set(headers) |
||
| 150 | .type('form')
|
||
| 151 | .send({
|
||
| 152 | xqlbmc: '', |
||
| 153 | xnxqid: sem, |
||
| 154 | xqlb: '' |
||
| 155 | }) |
||
| 156 | .end(function (err, iires) {
|
||
| 157 | if (err) {
|
||
| 158 | console.log((timeStamp() + 'Failed to reach exams page\n' + err.stack).red); |
||
|
1 ignored issue
–
show
|
|||
| 159 | res.send({ error: '获取成绩失败' });
|
||
| 160 | return next(err); |
||
| 161 | } |
||
| 162 | program.fullLog && console.log((timeStamp() + 'Successfully entered exams page.').green); |
||
| 163 | |||
| 164 | $ = cheerio.load(iires.text); |
||
| 165 | |||
| 166 | let exams = []; |
||
| 167 | |||
| 168 | $('#dataList tr').each(function (index) {
|
||
| 169 | if (index === 0) |
||
| 170 | return; |
||
| 171 | let item = $(this).find('td');
|
||
| 172 | let subject = {};
|
||
| 173 | subject.subject = escaper.unescape(item.eq(3).text()); |
||
| 174 | subject.time = escaper.unescape(item.eq(4).text()); |
||
| 175 | subject.location = escaper.unescape(item.eq(5).text()); |
||
| 176 | subject.seat = escaper.unescape(item.eq(6).text()); |
||
| 177 | exams.push(subject); |
||
| 178 | }); |
||
| 179 | |||
| 180 | ret.exams = exams; |
||
| 181 | |||
| 182 | access.logout(headers, res, function() {
|
||
| 183 | res.send(JSON.stringify(ret)); |
||
| 184 | program.fullLog && console.log((timeStamp() + 'Successfully logged out: ').green + req.query.id.yellow + (' (processed in ' + (new Date() - start) + 'ms)').green);
|
||
|
1 ignored issue
–
show
|
|||
| 185 | }); |
||
| 186 | }); |
||
| 187 | }); |
||
| 188 | }); |
||
| 189 | |||
| 191 | console.log((timeStamp() + 'The server is now running on port ' + port + '.').green); |